# -*- coding: utf-8 -*-
"""
Web framework/Tornado Get Request Params/test01.py
"""
import tornado.web
import asyncio
import json


class GetHandler(tornado.web.RequestHandler):
    def get(self):
        """
        获取**GET**请求参数
        :return:
        """
        p_get_argument = self.get_argument("name", default="Jack")
        p_get_arguments = self.get_arguments("age")
        p_get_query_argument = self.get_query_argument("name", default="Jack")
        p_get_query_arguments = self.get_query_arguments("name")
        r_message = f"self.get_argument: {p_get_argument}, " \
            f"self.get_arguments: {p_get_arguments}, " \
            f"self.get_query_argument: {p_get_query_argument}, " \
            f"self.get_query_arguments: {p_get_query_arguments}"
        self.finish(r_message)
        # > curl -s "http://127.0.0.1:8888/get/?name=Mack&age=20"
        # > HTTP Response: self.get_argument: Mack, self.get_arguments: ['20'],
        # self.get_query_argument: Mack, self.get_query_arguments: ['Mack']


class PostBodyHandler(tornado.web.RequestHandler):
    def post(self):
        """
        获取**POST**请求参数
        :return:
        """
        p_get_argument = self.get_argument("name", default="Jack")
        p_get_arguments = self.get_arguments("name")
        p_get_body_argument = self.get_body_argument("name", default="Jack")
        p_get_body_arguments = self.get_body_arguments("name")
        r_message = f"self.get_argument: {p_get_argument}, " \
            f"self.get_arguments: {p_get_arguments}, " \
            f"self.get_body_argument: {p_get_body_argument}, " \
            f"self.get_body_arguments: {p_get_body_arguments}"
        self.write(r_message)
        # > curl -X POST -s "http://127.0.0.1:8888/post-body/" -d "name=Mack" -d "age=20"
        # > HTTP Response: self.get_argument: Mack, self.get_arguments: ['Mack'],
        # self.get_body_argument: Mack, self.get_body_arguments: ['Mack']


class PostJsonHandler(tornado.web.RequestHandler):
    def post(self):
        """
        获取**POST**请求参数
        :return:
        """
        p_request_json = json.loads(self.request.body)
        self.write(p_request_json)
        # > curl -X POST -H "Content-Type: application/json" -s "http://127.0.0.1:8888/post-json/"
        # -d "{\"name\": \"Mack\", \"age\": 20}"
        # > HTTP Response: {"name": "Mack", "age": 20}


class PutHandler(tornado.web.RequestHandler):
    def put(self, data="This is default."):
        """
        获取**PUT**请求参数
        :return:
        """
        r_message = f"data: {data}"
        self.write(r_message)
        # > curl -X PUT -s "http://127.0.0.1:8888/put/helloworld
        # > HTTP Response: data: helloworld


async def main():
    app = tornado.web.Application([
        (r'/get/', GetHandler),
        (r'/post-body/', PostBodyHandler),
        (r'/post-json/', PostJsonHandler),
        (r'/put/([^/]+)', PutHandler),
    ])
    app.listen(8888)
    await asyncio.Event().wait()


if __name__ == '__main__':
    asyncio.run(main())
